<?php
// 本类由系统自动生成，仅供测试用途
//301登陆出错
//302 短信发送失败
//303 重复验证
//304 验证失败
//305 未激活
//306 cookiekey非法
//307 参数不完整
//308 上传失败
//309 手机号已注册
//310 课次不存在。
//311 已经阅读过公告
//312用户未审核
//304post数据不完整 305 发送失败 306 已验证过

class IndexAction extends CommonAction {

    public function _error($code)
    {
        if ($code ==306 || $code ==301)
        {
            cookie('key',null);
        }
        $action =$_REQUEST['action'];
        $arr[$action]['state'] = 'error';
        $arr[$action]['code'] = $code;
        echo json_encode($arr);
        exit;
    }
   
    public function index()
    {
        Load('extend');
       // echo encrypt('15800051216');
        //   echo '<br>';
        //  echo decrypt('GD39ujNTrwp0ZnNnkWtsRg==');

        echo header("Access-Control-Allow-Origin: *");
        $action = $_REQUEST['action'];
        $key =cookie('key');
        if (!empty($key))
        {
            
            header("Set-Cookie:key=".$key);
            $key = cookie('key');
            $keystr =decrypt($key);
            $keyarr =explode('###sc',$keystr);
            $userid = $keyarr[0];
            $phone = $keyarr[1];

            if (empty($userid))
            {
               
               $this ->_error(306);
            }
            $shenhe =getinfo($userid,'user','shenhe');
            if ($shenhe!=1)
            {
                $this ->_error(312);
            } 

        }
        else
        {
            if ($action != 'login' && $action !='getRegisterNO'  && $action !='postRegisterNO' )
            {
                
                $this ->_error(301);

            }
            
        }

        

      
        

        

        switch ($action) {

            case 'login':
                    $responsearr = $this->_login();
                    break;

            case 'logout':
                    $responsearr = $this->_logout();
                    break;

            case 'getRegisterNO':
                    $responsearr = $this->_getRegisterNO();
                    break;

            case 'postRegisterNO':
                    $responsearr = $this->_postRegisterNO();
                    break;

            case 'initPW':
                    $responsearr = $this->_initPW($userid);
                    break;

            case 'contactList':
                    $responsearr = $this->_contactList($userid);
                    break;

            case 'function':
                    $responsearr = $this->_function($userid);
                    break;

            case 'proclamationList':
                    $responsearr = $this->_proclamationList($userid);
                    break;

            case 'postProclamation':
                    $responsearr = $this->_postProclamation($userid);
                    break;

            case 'classList':
                    $responsearr = $this->_classList($userid);
                    break;

            case 'homeWorkList':
                    $responsearr = $this->_homeWorkList($userid);
                    break;
                    
            case 'setHomeWork':
                    $responsearr = $this->_setHomeWork($userid);
                    break;

            case 'commentList':
                    $responsearr = $this->_commentList($userid);
                    break;

            case 'postComment':
                    $responsearr = $this->_postComment($userid);
                    break; 

            case 'me':
                    $responsearr = $this->_me($userid);
                    break;

            case 'changeME':
                    $responsearr = $this->_changeME($userid);
                    break;

            case 'changeStudent':
                    $responsearr = $this->_changeStudent($userid);
                    break;

            case 'uploadpic':
                    $responsearr = $this->_uploadpic($userid);
                    break;

            case 'changePW':
                    $responsearr = $this->_changePW($userid);
                    break;

            case 'postAttence':
                    $responsearr = $this->_postAttence($userid);
                    break;

            case 'affirmProclamation':
                    $responsearr =$this->_affirmProclamation($userid);
                    break;

            case 'commentteacher':
                    $responsearr =$this->_commentteacher($userid);
                    break;


            case 'student':
                    $responsearr =$this->_student($userid);
                     break;
            case 'im':
                    $responsearr =$this->_im($userid);
                     break;
                    
            default:
                    # code...
                    break;
                
        }
        $json = json_encode($responsearr);
        // for ($i=0; $i < 20 ; $i++) { 
        //     # code...
        //     $json = str_replace('"'.$i.'":','',$json);

        // }
        //$json = str_replace('"0":','',$json);

        echo str_replace('null', '""',$json);

    }

    private function _affirmProclamation($userid)
    {
        //$parent_id=$_REQUEST['parent_id'];
       
        $parentinfo = loadinfo($userid,2);
        $proclamation_id =$_REQUEST['proclamation_id'];
        if (empty($parentinfo)  or empty($proclamation_id))
        {
            $this ->_error(307);
        }
        $mproclamation_log =M('proclamation_log');
        
        $data['parent_id'] =$parentinfo['id'];
        
        $data['proclamation_id'] =$proclamation_id;
        if ($mproclamation_log->where($data)->count()==0)
        {
            $mproclamation_log->add($data);
            $arr['affirmProclamation']['state'] = "success";
            return $arr;
        }
        else
        {
             $this ->_error(311);
        }
 
    }


    private function _login()
    {

        $map['SJHM'] = decrypt($_REQUEST['phone']);
        $map['password_des'] = $_REQUEST['pass'];
        
        $muser =M('user');

        $userinfo =$muser ->where($map)->find();
        
        //echo $muser->getlastsql();
        //print_r($userinfo);
        if (!empty($userinfo))
        {
            if ($userinfo['status']!=1)
            {
                $this ->_error();
            }
            $data['systype']=$_REQUEST['device_type'];
            $data['token']=$_REQUEST['device_token'];
            $vailkey = encrypt($userinfo['id'].'###sc'.$userinfo['SJHM']);
            cookie('key',$vailkey);
            $muser ->where('id = '.$userinfo['id'])->save($data);

                $arr['login']['state']='success';
                if ($userinfo['is_teacher']==1)
                {
                    $teacherinfo = loadinfo($userinfo['id'],1);
                    
                    $arr['login']['teacher']['teacher_id'] = $teacherinfo['id'];
                    $arr['login']['teacher']['user_id'] = $userinfo['id'];
                    $arr['login']['teacher']['teacher_url'] = C('PATH').'/'.$teacherinfo['pic'];
                    $arr['login']['teacher']['name'] = $teacherinfo['XM'];
                    $arr['login']['teacher']['sex'] = $teacherinfo['XBM'];
                    $arr['login']['teacher']['messageNO'] = 0;

                    $mclass =M('class');
                    $msubject =M('subject');
                    
                    //$classlist =$mclass->field('id,class_name as name')->where('teacher_id = '.$teacherinfo['id'])->select();
                    
                    $subjectlist =$msubject->field('id,KCMC as subject_name')->where('id in ( '.$teacherinfo['subject_id'].')')->select();
                    
                    $mclass = M('class');
                   
                    $classinfo = $mclass->field('class.id,class_name,class.intro as class_introduce,teacher.XM as class_manager') ->join('teacher ON teacher.id = class.teacher_id') ->where('teacher_id ='.$teacherinfo['id'])->select();
                 
                    $mtp = M('teacher_power');
                    $plist = $mtp->where('teacher_id ='.$teacherinfo['id'])->select();
                    foreach ($plist as $plt) {
                        $tplist .= $plt['class_id'].',';
                        # code...
                    }
                    $tplist = rtrim ($tplist,',');
                    $classlist = $mclass ->field('class.id,class_name,class.intro as class_introduce,teacher.XM as class_manager') ->join('teacher ON teacher.id = class.teacher_id')  ->where("class.id in (".$tplist.")")->select();


                    $arr['login']['teacher']['class']['ctrl_class'] = $classinfo;
                    $arr['login']['teacher']['class']['power_list'] = $classlist;

                    //$arr['login']['teacher']['class'] =$classlist;
                    $arr['login']['teacher']['subject'] =$subjectlist;
                    
                    //$arr['login']['teacher']['class'] = getclass($teacherinfo['ctrl_classid']);
                    $arr['login']['teacher']['school'] = getschool($teacherinfo['school_id']);
                }
            
                if ($userinfo['is_parent']==1)
                {
                    $mparent = M('parent');
                    $parentinfo = loadinfo($userinfo['id'],2);
                    $arr['login']['parent']['parent_id'] = $parentinfo['id'];
                    $arr['login']['parent']['user_id'] = $userinfo['id'];
                    $arr['login']['parent']['parent_url'] = C('PATH').'/'.$parentinfo['pic'];
                    $arr['login']['parent']['name'] = $parentinfo['XM'];
                    $arr['login']['parent']['sex'] = $parentinfo['XBM'];
                    $mstudent = M('student');
                    $studentlist = $mstudent ->field("student.id,student.XM as name,student.class_id,student.school_id ")->join('parent_power on parent_power.student_id = student.id') ->where('parent_power.parent_id='.$parentinfo['id'])->select();
                    for($i=0;$i<count($studentlist);$i++)
                    {
                        $studentlist[$i]['class']=getclass($studentlist[$i]['class_id']);
                        $studentlist[$i]['school']=getschool($studentlist[$i]['school_id']);
                        
                    }
                    $arr['login']['parent']['student'] =$studentlist;
                }
            
        }
        else
        {
           $this ->_error(301);
        }
        return $arr;
  
    }

    private function _logout()
    {
        $arr['logout']['state'] = "success";
        cookie('key',null);
        return $arr;
        exit;
    }

    private function _getRegisterNO()
    {
        $map['SJHM'] = decrypt($_REQUEST['phone']);
	    //$map['SJHM'] = str_replace("+86","",$_REQUEST['phone']);
        $muser =M('user');
        $userinfo =$muser ->where($map)->find();
        $str = $this->randchar();
       // $str = '1234';
        $data['vcode'] = $str;
        $muser ->where($map)->save($data);
        $userinfo =$muser ->where($map)->find();
       // echo $muser->getlastsql();
        if (!empty($userinfo))
        {
            if ($userinfo['status'] == 0)
            {
                $sms = $this->sendsms($map['SJHM'],'验证码：'.$userinfo['vcode']);
               // $sms = $this->sendsms($map['SJHM'],$userinfo['vcode']);

                if ($sms!=1)
                {
                    $this ->_error(302);
                }
                else
                {
                    $arr['getRegisterNO']['state'] = "success"; 
                }
            }
            else
            {
                $this ->_error(303);
            }
            return $arr;
        }

    }

    private function _postRegisterNO()
    {
        $map['SJHM'] = decrypt($_REQUEST['phone']);
        //$map['SJHM'] = $_REQUEST['phone'];
        $map['vcode'] = $_REQUEST['RegisterNO'];
       // $map['status'] = 0;
        $muser =M('user');
        $userinfo = $muser ->where($map)->find();
        if (!empty($userinfo))
        {
            $data['status'] =1 ;
            $data['id'] = $userinfo['id'];
            $muser -> save($data);
            

            //登陆方法

            $data['systype']=$_REQUEST['device_type'];
            $data['token']=$_REQUEST['device_token'];
            $vailkey = encrypt($userinfo['id'].'###sc'.$userinfo['SJHM']);
            cookie('key',$vailkey);
            $muser ->where('id = '.$userinfo['id'])->save($data);

            if (loadm($userinfo['id'],1))
            {
                $teacherinfo = loadinfo($userinfo['id'],1);
                $arr['postRegisterNO']['state']='success';
                $arr['postRegisterNO']['teacher']['teacher_id'] = $teacherinfo['id'];
                $arr['postRegisterNO']['teacher']['user_id'] = $userinfo['id'];
                $arr['postRegisterNO']['teacher']['teacher_url'] = C('PATH').'/'.$teacherinfo['pic'];
                $arr['postRegisterNO']['teacher']['name'] = $teacherinfo['XM'];
                $arr['postRegisterNO']['teacher']['sex'] = $teacherinfo['XBM'];
                $arr['postRegisterNO']['teacher']['messageNO'] = 0;
/*                 $mclass =M('class');
                $classlist =$mclass->field('id,class_name as name')->where('teacher_id = '.$teacherinfo['id'])->select();
                $arr['postRegisterNO']['teacher']['class'] =$classlist;
                //$arr['login']['teacher']['class'] = getclass($teacherinfo['ctrl_classid']);
                $arr['postRegisterNO']['teacher']['school'] = getschool($teacherinfo['school_id']); */
				
                    $mclass =M('class');
                    $msubject =M('subject');
                    
                    //$classlist =$mclass->field('id,class_name as name')->where('teacher_id = '.$teacherinfo['id'])->select();
                    
                    $subjectlist =$msubject->field('id,KCMC as subject_name')->where('id in ( '.$teacherinfo['subject_id'].')')->select();
                    
                    $mclass = M('class');
                   
                    $classinfo = $mclass->field('class.id,class_name,class.intro as class_introduce,teacher.XM as class_manager') ->join('teacher ON teacher.id = class.teacher_id') ->where('teacher_id ='.$teacherinfo['id'])->select();
                 
                    $mtp = M('teacher_power');
                    $plist = $mtp->where('teacher_id ='.$teacherinfo['id'])->select();
                    foreach ($plist as $plt) {
                        $tplist .= $plt['class_id'].',';
                        # code...
                    }
                    $tplist = rtrim ($tplist,',');
                    $classlist = $mclass ->field('class.id,class_name,class.intro as class_introduce,teacher.XM as class_manager') ->join('teacher ON teacher.id = class.teacher_id')  ->where("class.id in (".$tplist.")")->select();


                    $arr['postRegisterNO']['teacher']['class']['ctrl_class'] = $classinfo;
                    $arr['postRegisterNO']['teacher']['class']['power_list'] = $classlist;
					
                    $arr['postRegisterNO']['teacher']['subject'] =$subjectlist;
                    
                    //$arr['login']['teacher']['class'] = getclass($teacherinfo['ctrl_classid']);
                    $arr['postRegisterNO']['teacher']['school'] = getschool($teacherinfo['school_id']);
				
            }
            if (loadm($userinfo['id'],2))
            {
                $mparent = M('parent');
                $parentinfo = loadinfo($userinfo['id'],2);
                $arr['postRegisterNO']['parent']['parent_id'] = $parentinfo['id'];
                $arr['postRegisterNO']['parent']['user_id'] = $userinfo['id'];
                $arr['postRegisterNO']['parent']['parent_url'] = C('PATH').'/'.$parentinfo['pic'];
                $arr['postRegisterNO']['parent']['name'] = $parentinfo['XM'];
                $arr['postRegisterNO']['parent']['sex'] = $parentinfo['XBM'];
                $mstudent = M('student');
/*                 $studentlist = $mstudent ->field("id,XM as name ") ->where('parent_id='.$teacherinfo['id'])->select();
                for($i=0;$i<count($studentlist);$i++)
                {
                    $studentlist[$i]['class']=getclass($teacherinfo['class_id']);
                    $studentlist[$i]['school']=getclass($teacherinfo['school_id']);
                    
                } */
                    // $studentlist = $mstudent ->field("id,XM as name,class_id,school_id ") ->where('parent_id='.$parentinfo['id'])->select();
                     $studentlist = $mstudent ->field("student.id,student.XM as name,student.class_id,student.school_id ")->join('parent_power on parent_power.student_id = student.id') ->where('parent_power.parent_id='.$parentinfo['id'])->select();
                    for($i=0;$i<count($studentlist);$i++)
                    {
                        $studentlist[$i]['class']=getclass($studentlist[$i]['class_id']);
                        $studentlist[$i]['school']=getschool($studentlist[$i]['school_id']);
                        
                    }


                $arr['postRegisterNO']['parent']['student'] =$studentlist;

            }

            //$arr['postRegisterNO']['state'] = "success";
            //$arr = $this ->_selectmember($userinfo['id']);
        }
        else
        {
            $this ->_error(304);
        } 
        return $arr;
    }

    private function _initPW($userid)
    {
     
        $map['id'] = $userid;
        $data['password_des'] = $_REQUEST['pw'];
        $muser =M('user');
		$aa = $muser ->where($map)->select();
		
       if ($aa){	  
			$muser->where($map)->save($data);
            $arr['initPW']['state'] = "success";
            //cookie('key',null);
        }else{
			exit('not user');
		}
		 
        return $arr;
    }

    private function _changePW($userid)
    {
       
        $map['id'] = $userid;
        $map['password_des'] = $_REQUEST['oldPW'];
        $data['password_des'] = $_REQUEST['newPW'];
        $muser =M('user');
		$aa = $muser ->where($map)->select();
       if ($aa){	  
			$muser->where($map)->save($data);
            $arr['changePW']['state'] = "success";
            //cookie('key',null);
        }else{
			exit('not user');
 		}
/*        if ($muser ->where($map)->save($data))
        {
            $arr['changePW']['state'] = "success";
            //cookie('key',null);
        } */
        return $arr;
    }

    private function _contactList($id)
    {
        $type =$_REQUEST['type'];
        if ($type == 'teacher')
        {
            $teacherinfo = loadinfo($id,1);
            $school_id =$teacherinfo['school_id'];
            $map['school_id'] = $school_id;
            $mteacher = M('teacher');
            $mclass = M('class');

            $path =C('PATH');
            $teacherlist = $mteacher ->field("user.id as user_id,XM as teacher_name,XBM as teacher_sex,SJHM as teacher_phone,intro as teacher_intro,CONCAT('".$path."/',pic) as teacher_url")->join('user ON user.id = teacher.user_id') ->where($map)->where('user.id !='.$id)->order('CONVERT(teacher.XM USING gbk)') ->select();
           // echo $mteacher ->getlastsql();

            $classinfo =$mclass->field('id,class_name as class_name')->where('teacher_id = '.$teacherinfo['id'])->select();

            foreach ($classinfo as $ccl) {
                $cclist .= $ccl['id'].',';
                # code...
            }
            $cclist =rtrim($cclist,',');
            if ($cclist=='')
            {
                $cclist='0';
            }
            $arr['contactList']['state']="success";
            $arr['contactList']['type']="teacher";
           // echo $mclass ->getlastsql();
            $arr['contactList']['teacher'] = $teacherlist; //teacherlist
            
            $mstudent = M('student');
            $mparent =M('parent');
            for($i=0;$i<count($classinfo);$i++)
            {

                //$studentlist = $mstudent ->field("user.id as user_id,student.id as student_id,student.XM as student_name,parent.XM as parent_name,parent.XBM as parent_sex,parent.intro as parent_intro,user.SJHM as parent_phone, CONCAT('".$path."/',parent.pic)  as parent_url") ->join('parent ON parent.id = student.parent_id') ->join('user ON user.id = parent.user_id')->order('CONVERT(parent.XM USING gbk)')  ->where('class_id ='.$classinfo[$i]['id'])->select();
                $studentlist = $mparent ->field("user.id as user_id,student.id as student_id,student.XM as student_name,parent.XM as parent_name,parent.XBM as parent_sex,parent.intro as parent_intro,user.SJHM as parent_phone, CONCAT('".$path."/',parent.pic)  as parent_url")->join('user ON user.id = parent.user_id')->join('parent_power ON parent.id = parent_power.parent_id') ->join('student ON parent_power.student_id = student.id')->where('student.class_id ='.$classinfo[$i]['id'])->order('CONVERT(parent.XM USING gbk)')->select();

                //echo $mstudent ->getlastsql();

                $classinfo[$i]['parent'] =$studentlist;

                //$cclist .= $classinfo[$i]['class_id'].',';
                //$mresultlist = $mstudent_result ->field('exam_id,exam_name,subject_id,KCMC as subject_name,FSLKSCJ as result') ->join('subject ON subject.id = student_result.subject_id') ->join('exam ON exam.id = student_result.exam_id') ->where($map1) -> select();
            }
            $mtp = M('teacher_power');
            $plist = $mtp->where('teacher_id ='.$teacherinfo['id'])->select();
            foreach ($plist as $plt) {
                $tplist .= $plt['class_id'].',';
                # code...
            }
            $tplist = rtrim ($tplist,',');
            $classlist = $mclass ->field('id,class_name as class_name ') ->where("id in (".$tplist.") and id not in(".$cclist.")")->select();
            
            for($i=0;$i<count($classlist);$i++)
            {
                //$studentlist = $mstudent ->field("user.id as user_id,student.id as student_id,student.XM as student_name,parent.XM as parent_name,parent.XBM as parent_sex,parent.intro as parent_intro,user.SJHM as parent_phone, CONCAT('".$path."/',parent.pic)  as parent_url") ->join('parent ON parent.id = student.parent_id') ->join('user ON user.id = parent.user_id')->order('CONVERT(parent.XM USING gbk)')  ->where('class_id ='.$classlist[$i]['id'])->select();
                $studentlist = $mparent ->field("user.id as user_id,student.id as student_id,student.XM as student_name,parent.XM as parent_name,parent.XBM as parent_sex,parent.intro as parent_intro,user.SJHM as parent_phone, CONCAT('".$path."/',parent.pic)  as parent_url") ->join('user ON user.id = parent.user_id')->join('parent_power ON parent.id = parent_power.parent_id') ->join('student ON parent_power.student_id = student.id')->where('student.class_id ='.$classlist[$i]['id'])->order('CONVERT(parent.XM USING gbk)')->select();
               

                $classlist[$i]['parent'] =$studentlist;
                
            }
            $arr['contactList']['parent']['ctrl_class']=$classinfo;
            $arr['contactList']['parent']['power_list'] = $classlist;
            
            return $arr;
        






        }
        else if ($type == 'parent')
        {
            $parentinfo = loadinfo($id,2);
            $path =C('PATH');
            $mstudent = M('student');
            $mteacher = M('teacher');
            $mparent = M('parent');
            $mclass = M('class');
            $studentlist = $mstudent ->field('student.id as student_id,student.XM as student_name,student.class_id,class.class_name')->join('class ON class.id = student.class_id')->join('parent_power ON parent_power.student_id = student.id') ->where('parent_power.parent_id='.$parentinfo['id'])->select();
           // echo $mstudent->getlastsql();       
            for($i=0;$i<count($studentlist);$i++)
            {
                $studentlist_1 = $mparent->field("user.id as user_id,student.id as student_id,student.XM as student_name,parent.XM as parent_name,parent.XBM as parent_sex,parent.intro as parent_intro,user.SJHM as parent_phone, CONCAT('".$path."/',parent.pic)  as parent_url") ->join('user ON user.id = parent.user_id')->join('parent_power ON parent.id = parent_power.parent_id') ->join('student ON parent_power.student_id = student.id')  ->where('student.class_id ='.$studentlist[$i]['class_id'].' and student.id != '.$studentlist[$i]['student_id'])->order('CONVERT(parent.XM USING gbk)')->select();
                $studentlist[$i]['parent'] =$studentlist_1;

                $classinfo = $mclass ->find($studentlist[$i]['class_id']);


                //echo $mclass->getlastsql();
                $teacherid =$classinfo['teacher_id'];
                $mtp = M('teacher_power');
                $plist = $mtp->where('class_id ='.$classinfo['id'])->select();
                foreach ($plist as $plt) {
                    $tplist .= $plt['teacher_id'].',';
                    # code...
                }
                $tplist = rtrim ($tplist,','); 
                if (!empty($tplist))
                {
                    $finalteacherid = $tplist.','.$teacherid;
                }
                else
                {
                    $finalteacherid =$teacherid;
                }


                $teacherinfo  = $mteacher ->field("user.id as user_id,XM as teacher_name,XBM as teacher_sex,SJHM as teacher_phone,intro as teacher_intro,CONCAT('".$path."/',pic) as teacher_url")->join('user ON user.id = teacher.user_id')->where('teacher.id in ('.$finalteacherid.')')->order('CONVERT(teacher.XM USING gbk)')->select();
               
                $studentlist[$i]['teacher'] =$teacherinfo;

            }
            $arr['contactList']['state']="success";
            $arr['contactList']['type']="parent";
            $arr['contactList']['child_list'] =$studentlist;
            return $arr;

        }
    }

        private function _function($id)
        {
            $type =$_REQUEST['type'];
            if(empty($type))
            {
                $this ->_error(307);
            }

            $arr['function']['state']='success';
            $arr['function']['type'] =$type;
            
            if ($type=='teacher')
            {
                $arr['function']['classlesson_url']=C('PATH').'/api.php/Index/alllesson/type/teacher/id/'.$id;
                $arr['function']['mylesson_url']=C('PATH').'/api.php/Index/lesson/type/teacher/id/'.$id;

                $teacherinfo = loadinfo($id,1);
                if (empty($teacherinfo))
                {
                    $this ->_error(307);
                }
                $arr['function']['results_url']=C('PATH').'/api.php/Index/result/type/teacher/id/'.$id;
                
                $mclass = M('class');
                $mstudent =M('student');
                $classinfo = $mclass->field('id as class_id,class_name')->where('teacher_id ='.$teacherinfo['id'])->select();
                
                $mtp = M('teacher_power');
                $plist = $mtp->where('teacher_id ='.$teacherinfo['id'])->select();
                foreach ($plist as $plt) {
                    $tplist .= $plt['class_id'].',';
                    # code...
                }
                $tplist = rtrim ($tplist,',');
                $classlist = $mclass ->field('id as class_id,class_name') ->where("id in (".$tplist.")")->select();
                $list['ctrl_class'] = $classinfo;
                $list['power_list'] = $classlist;
                $arr['function']['attence'] =$list;

                $mlesson_type =M('lesson_type');
                
                $mapsc['school_id']=$teacherinfo['school_id'];
                $mlesson_type =M('lesson_type');
                $lessonlist =$mlesson_type->field('id as lesson_type_id,type_name as lesson_type_name')->where($mapsc)->order('num asc')->select();
                
                $arr['function']['lesson_type']=$lessonlist;
            }
            if ($type=='parent')
            {
                $parentinfo = loadinfo($id,2);
                $arr['function']['lesson_url']=C('PATH').__URL__.'/lesson/type/parent/id/'.$id;
               
                $mstudent =M('student');
                $map['parent_id'] = $parentinfo['id'];
                $studentlist = $mstudent ->field('student.id as student_id,student.XM as student_name')->join('parent_power on parent_power.student_id =student.id')->where('parent_power.parent_id='.$parentinfo['id'])->select();
                for($i=0;$i<count($studentlist);$i++)
                {
                    $studentlist[$i]['url'] =  C('PATH').__URL__.'/result/type/parent/id/'.$id.'/student_id/'.$studentlist[$i]['student_id']; 
                }
                $arr['function']['results_url'] =$studentlist;
               // $arr['function']['results_url']=C('PATH').__URL__.'/result/type/parent/id/'.$id; 
                $arr['function']['attence_url']=C('PATH').__URL__.'/attence/id/'.$id;
            }
           
            return $arr;
        }

        
        
       
        private function _proclamationList($id)
        {
            //echo 112;
            $page = loadpage(20);

            
            $mclass =M('class');
            $class_id =$_REQUEST['class_id'];
            if (empty($class_id))
            {
                $this ->_error(307);
            }
            $classinfo =$mclass ->find($class_id);
            $school_id =$classinfo['school_id'];
            $arr['proclamationList']['state']='success';
            // $schoollist = getproclamation(1,$school_id,$page);//
            // $classlist = getproclamation(2,$class_id,$page);//
            
            $sql = "select id as proclamation_id,title  as proclamation_title,content as proclamation_content,FBSJ as proclamation_time,teacher_id,class_id from class_proclamation where class_proclamation.class_id = ".$class_id." UNION  all SELECT id  as proclamation_id,title as proclamation_title,content  as proclamation_content,FBSJ  as proclamation_time,teacher_id,'school' as class_id from school_proclamation  where school_proclamation.school_id = ".$school_id." ORDER BY proclamation_time desc limit ".$page[0].",".$page[1];
           //echo $sql;
            $m=M('');

            $sql2 = "select id as proclamation_id,title  as proclamation_title,content as proclamation_content,FBSJ as proclamation_time,teacher_id from class_proclamation where class_proclamation.class_id = ".$class_id." UNION  all SELECT id  as proclamation_id,title as proclamation_title,content  as proclamation_content,FBSJ  as proclamation_time,teacher_id from school_proclamation  where school_proclamation.school_id = ".$school_id." ORDER BY proclamation_time desc limit ".$page[2]*$page[1].",".$page[1];
            $newlist = $m->query($sql2);
            if (empty($newlist))
            {
                $isNext =0;
            }
            else
            {
                $isNext =1;
            }

            //$nowcount = count($scprolist)+$page[2]*$page[1];
            
            $mstudent =M('student');
            $mparent =M('parent');
            $mproclamation_log =M('proclamation_log');
            $list = $m ->query($sql);
            //print_r($list);
            for($i=0;$i<count($list);$i++)
            {
                if ($list[$i]['class_id']!='school')
                {
                    $list[$i]['proclamation_url'] =C('PATH').__URL__.'/view/type/clsproclamation/id/'.$list[$i]['proclamation_id'];
                    $list[$i]['proclamation_promulgator'] =getinfo($list[$i]['teacher_id'],'teacher','XM');
                    $studentlist = $mparent ->field('parent.XM as student_name,parent.id as parent_id')->join('parent_power ON parent.id = parent_power.parent_id')->join('student ON student.id = parent_power.student_id')->where('student.class_id='.$list[$i]['class_id'])->order('CONVERT(parent.XM USING gbk)')->select();
                    //$studentlist =$mstudent ->field('student.XM as student_name,student.parent_id') ->where('student.class_id='.$list[$i]['class_id'])->join("parent_power on parent.id =student.parent_id")->order('CONVERT(parent.XM USING gbk)')->select();

                    for($j=0;$j<count($studentlist);$j++)
                    {
                        $plc= $mproclamation_log ->where('parent_id='.$studentlist[$j]['parent_id'])->count();
                        if ($plc>0)
                        {
                            $studentlist[$j]['isAffirm'] = 1;
                        }
                        else
                        {
                            $studentlist[$j]['isAffirm'] = 0;
                        }
                    }
                    $list[$i]['prarent_list'] =$studentlist;
                    
                }
                else
                {
                    $list[$i]['proclamation_url'] =C('PATH').__URL__.'/view/type/schproclamation/id/'.$list[$i]['proclamation_id'];
                }
                $list[$i]['proclamation_content'] = msubstr($list[$i]['proclamation_content'],0,30);
                
            }
            $arr['proclamationList']['list'] = $list;

            //$arr['proclamationList']['school_list'] =$schoollist;
            //$arr['proclamationList']['class_list'] =$classlist;
            $arr['proclamationList']['isNext'] = $isNext;
            $arr['proclamationList']['pageNo'] = $page[2];
            return $arr;
            //echo $school_id;

        }

        private function _postProclamation($id)
        {
            if ($_REQUEST['type'] =='school')
            {
                $teacherinfo = loadinfo($id,1);
                $data['proclamation_type_id'] = $_REQUEST['proclamation_type'];
                $data['title'] = $_REQUEST['title'];
                $data['content'] = $_REQUEST['content'];
                $data['school_id'] = $teacherinfo['school_id'];
                $data['teacher_id'] = $teacherinfo['id'];
                $data['FBSJ'] = time();
                $m= M('school_proclamation');
                if ($m->add($data))
                {
                    $arr['postProclamation']['state'] = "success";
                }
            }
            else if ($_REQUEST['type'] =='class')
            {
                $teacherinfo = loadinfo($id,1);
                $data['proclamation_type_id'] = $_REQUEST['proclamation_type'];
                $data['title'] = $_REQUEST['title'];
                $data['content'] = $_REQUEST['content'];
                $data['class_id'] = $_REQUEST['class_id'];
                $data['teacher_id'] = $teacherinfo['id'];
                
                $data['FBSJ'] = time();
                $m= M('class_proclamation');
                if ($m->add($data))
                {
                    $arr['postProclamation']['state'] = "success";
                }
            }
            return $arr;    
        }

        private function _classList($id)
        {
            $teacherinfo = loadinfo($id,1);

            $mclass = M('class');
            $mstudent =M('student');
            $path=C('PATH');
            $classinfo = $mclass->field('id as class_id,class_name')->where('teacher_id ='.$teacherinfo['id'])->select();
            for($i=0;$i<count($classinfo);$i++)
            {
                $studentlist = $mstudent ->field('id,XM as student_name'.",CONCAT('".$path."/',pic) as student_url")->where('class_id = '.$classinfo[$i]['class_id'])->order('CONVERT(student_name USING gbk)')->select();
                    //$studentlist[$i]['classinfo']=getclass($teacherinfo['class_id']);
                   $classinfo[$i]['student']=$studentlist;
                    
            }
            $mtp = M('teacher_power');
            $plist = $mtp->where('teacher_id ='.$teacherinfo['id'])->select();
            foreach ($plist as $plt) {
                $tplist .= $plt['class_id'].',';
                # code...
            }
            $tplist = rtrim ($tplist,',');
            $classlist = $mclass ->field('id as class_id,class_name') ->where("id in (".$tplist.")")->select();

            for($i=0;$i<count($classlist);$i++)
            {
                $studentlist1 = $mstudent ->field('id,XM as student_name'.",CONCAT('".$path."/',pic) as student_url")->where('class_id = '.$classlist[$i]['class_id'])->order('CONVERT(student_name USING gbk)')->select();
                    //$studentlist[$i]['classinfo']=getclass($teacherinfo['class_id']);
                   $classlist[$i]['student']=$studentlist1;
                    
            }
            $arr['state']='success';
            $arr['ctrl_class'] = $classinfo;
            $arr['power_list'] = $classlist;
            $return['classList'] =$arr;
            return $return;
        }

        private function _homeWorkList($id)
        {
            $page = loadpage(20);
            $class_id =$_REQUEST['class_id'];
            $mm =$_REQUEST['type'];
            
            if (empty($class_id) or empty($mm))
            {
                $this ->_error(307);
            }
            //echo 1231;
            if ($mm=='teacher')
            {
                $teacherinfo = loadinfo($id,1);

                $work =M('student_work');
                $worklist = $work ->join('work_type ON work_type.id = student_work.work_type_id')->join('teacher ON teacher.id = student_work.teacher_id')->field('student_work.id as work_id,teacher.XM as homework_promulgator,title as homework_title,content as homework_content,FBSJ as homework_time,student_work.subject_id')->where('student_work.class_id ='.$class_id.' and student_work.teacher_id='.$teacherinfo['id'])->limit($page[0],$page[1])->order('student_work.id desc')->select();

                for($i=0;$i<count($worklist);$i++)
                {
                    $worklist[$i]['homework_url'] =C('PATH').__URL__.'/view/type/homework/id/'.$worklist[$i]['work_id'];
                    $worklist[$i]['subject'] = getinfo($worklist[$i]['subject_id'],'subject','KCMC');

                }
                $count = $work ->where('student_work.class_id ='.$class_id.' and student_work.teacher_id='.$teacherinfo['id']) ->count();

            }
            if ($mm=='parent')
            {
                $work =M('student_work');

                $worklist = $work ->join('work_type ON work_type.id = student_work.work_type_id')->join('teacher ON teacher.id = student_work.teacher_id')->field('student_work.id as work_id,teacher.XM as homework_promulgator,title as homework_title,content as homework_content,FBSJ as homework_time,student_work.subject_id')->where('student_work.class_id ='.$class_id)->limit($page[0],$page[1])->order('student_work.id desc')->select();
                $count = $work ->where('student_work.class_id ='.$class_id) ->count();
                for($i=0;$i<count($worklist);$i++)
                {
                    $worklist[$i]['homework_url'] =C('PATH').__URL__.'/view/type/homework/id/'.$worklist[$i]['work_id'];
                    $worklist[$i]['subject'] = getinfo($worklist[$i]['subject_id'],'subject','KCMC');

                }
                $count = $work ->where('student_work.class_id ='.$class_id) ->count();

            }
            $return['homeWorkList']['state'] = 'success';
            $return['homeWorkList']['list'] = $worklist;
            $return['homeWorkList']['pageNo'] = $page[2];
            
            $nowcount = count($worklist);
            
            //$nowcount = count($worklist)$page[2]*$page[1];
            if ($count>$nowcount)
            {
                $isNext =1;
            }
            else
            {
                $isNext =0;
            }
            $return['homeWorkList']['isNext']=$isNext;

            return $return; 
        
        }
        private function _setHomeWork($id)
        {
            $class_id =$_REQUEST['class_id'];

            $title =$_REQUEST['title'];
            $content =$_REQUEST['content'];
            $subject_id =$_REQUEST['subject_id'];
            
            if (empty($class_id) or empty($title)  or empty($content) or empty($subject_id))
            {
                $this ->_error(307);
            }
            $teacherinfo = loadinfo($id,1);
            $data['title'] = $_REQUEST['title'];
            $data['subject_id'] = $_REQUEST['subject_id'];
            $data['content'] = $_REQUEST['content'];
            $data['class_id'] = $_REQUEST['class_id'];
            $data['teacher_id'] = $teacherinfo['id'];    
            $data['FBSJ'] = time();
            $work =M('student_work');
            $work ->add($data);
            $arr['setHomeWork']['state']='success';
            return $arr;
               
            
        }

        private function _commentteacher($id)
        {
            $student_id = $_REQUEST['student_id'];
            if (empty($student_id))
            {
                $this ->_error(307);
            }
            $mteacher =M('teacher');
            $map['student_id'] =$student_id;
             $path =C('PATH');

            $list = $mteacher ->field("id as teacher_id,teacher.XM as teacher_name,CONCAT('".$path."/',teacher.pic) as teacher_url")->where("teacher.id in (select teacher_id from student_remark where student_id = ".$student_id.")")->order('CONVERT(teacher.XM USING gbk)')->select();
            //echo $mteacher->getlastsql();
            $arr['commentteacher']['state'] ='success';
            $arr['teacherlist'] = $list;
            return $arr;


        }

        private function _commentList($id)
        {
            $page = loadpage(20);
            $path =C('PATH');

            $mstudent_remark = M('student_remark');
            $map['student_id'] = $_REQUEST['student_id'];
            $map['_string'] ='(student_remark.teacher_id='.$_REQUEST['teacher_id'].' or student_remark.to_teacher_id ='.$_REQUEST['teacher_id'].')';

           // $map['teacher_id'] =array($_REQUEST['teacher_id'], 'or');
            //$map['to_teacher_id']=array('or', $_REQUEST['teacher_id']);
            //$map['teacher_id'] = $_REQUEST['teacher_id'];
            if (empty($map['student_id']) or empty($_REQUEST['teacher_id']))
            {
                $this ->_error(307);
            }

            $remarklist = $mstudent_remark ->where($map) ->field("teacher_id,teacher.XM as teacher_name,CONCAT('".$path."/',teacher.pic) as teacher_url, student_remark.parent_id,parent.XM as parent_name,CONCAT('".$path."/',parent.pic) as parent_url,student_remark.student_id,student.XM as student_name,JXPY as content,student_remark.FBSJ as time,teacher_or_parent") ->join('teacher ON teacher.id = student_remark.teacher_id') ->join('parent ON parent.id = student_remark.parent_id') ->join('student ON student.id = student_remark.student_id') ->limit($page[0],$page[1]) ->order('student_remark.FBSJ desc') ->select();
           // echo $mstudent_remark ->getlastsql();
            $arr['state'] ='success';
            $arr['commentList'] = $remarklist;
            $arr['pageNo'] = $page[2];
            $count = $mstudent_remark ->where($map) ->count();
            $nowcount = count($remarklist)+$page[2]*$page[1];
            if ($count>$nowcount)
            {
                $isNext =1;
            }
            else
            {
                $isNext =0;
            }
            $arr['isNext']=$isNext;
            return $arr;
        }

        private function _student($id)
        {
           $class_id =$_REQUEST['class_id'];
           if (empty($class_id))
           {
                $this->_error(307);
           }
           $mstudent = M('student');
           $studentlist =$mstudent->field('id as student_id ,XM as student_name')->where('class_id='.$class_id)->order('CONVERT(student.XM USING gbk)')->select();
           $arr['student']['state']= 'success';
           $arr['student']['list']= $studentlist;
           return $arr;

        }

        private function _postComment($id)
        {
            $mstudent_remark = M('student_remark');
            $student_id = $_REQUEST['student_id'];
            $student_id = trim($student_id,'[');
            $student_id = trim($student_id,']');
            $student_id = str_replace('"','',$student_id);
            $map['content'] =$student_id;
            $mc = M('content');
            $mc->add($map);
            $data['FBSJ'] = time();
            $data['JXPY'] = $_REQUEST['content'];
            $mm =$_REQUEST['type'];
            if (empty($mm) or empty($student_id)  or empty($_REQUEST['content']))
            {
                $this ->_error(307);
            }
            if ($mm=='teacher')
            {
                $teacherinfo = loadinfo($id,1);
                $data['teacher_id'] = $teacherinfo ['id'];
                $data['teacher_or_parent'] = 1;
            }
            else if ($mm=='parent')
            {
                if (empty($_REQUEST['teacher_id']))
                {
                    $this->_error(307);   
                }
                $parentinfo = loadinfo($id,2);
                $data['parent_id'] = $parentinfo ['id'];
                $data['to_teacher_id'] =$_REQUEST['teacher_id'];
                $data['teacher_or_parent'] = 2;

            }

            $stuarr =explode(',',$student_id);
            for($i=0;$i<count($stuarr);$i++)
            {
                $data['student_id'] =$stuarr[$i];
                $data['class_id'] =getinfo($data['student_id'],'student','class_id');
                $data['school_id'] =getinfo($data['student_id'],'student','school_id');
                $mstudent_remark ->add($data);
            }
            $arr['postcomment']['state'] = "success";
            // if($mstudent_remark ->add($data))
            // {
            //     $arr['postcomment']['state'] = "success";
            // }
            return $arr;
        }

        private function _me($id)
        {
            $mm =$_REQUEST['type'];
            if ($mm=='teacher')
            {
                $path =C('PATH');
                $teacherinfo = loadinfo($id,1);
                $mteacher =M('teacher');
                $map['teacher.id'] =$teacherinfo['id'];
                $teacher = $mteacher ->field("teacher.id as teacher_id,XM as teacher_name,XBM as teacher_sex,teacher_no,LXDH as teacher_tel,LXDZ as teacher_addr,QQ as teacher_qq,SJHM as teacher_phone,DZXX as teacher_email,CONCAT('".$path."/',teacher.pic) as teacher_url,subject_id,teacher.intro as teacher_intro,user_id,school_id")->join('user ON user.id = teacher.user_id') ->where($map)->find();
                $msubject =M('subject');
                $subjectlist =$msubject ->field('KCMC as subject_name')->where("id in(".$teacher['subject_id'].")")->select();

                $teacherinfo = loadinfo($id,1);

                $mclass = M('class');
               
                $classinfo = $mclass->field('class.id,class_name,class.intro as class_introduce,teacher.XM as class_manager') ->join('teacher ON teacher.id = class.teacher_id') ->where('teacher_id ='.$teacherinfo['id'])->select();
             
                $mtp = M('teacher_power');
                $plist = $mtp->where('teacher_id ='.$teacherinfo['id'])->select();
                foreach ($plist as $plt) {
                    $tplist .= $plt['class_id'].',';
                    # code...
                }
                $tplist = rtrim ($tplist,',');
                $classlist = $mclass ->field('class.id,class_name,class.intro as class_introduce,teacher.XM as class_manager') ->join('teacher ON teacher.id = class.teacher_id')  ->where("class.id in (".$tplist.")")->select();
                $arr['me']['state']='success';
                $arr['me']['type']='teacher';
                $arr['me']['teacher']=$teacher;
                $arr['me']['teacher']['subject']=$subjectlist;
                $arr['me']['teacher']['school'] =$this->_loadschool($teacher['school_id']);
                $arr['me']['teacher']['class']['ctrl_class'] = $classinfo;
                $arr['me']['teacher']['class']['power_list'] = $classlist;
               

            }
            else if ($mm=='parent')
            {

                $parentinfo = loadinfo($id,2);
                $path =C('PATH');
                $mparent =M('parent');
                $map['parent.id'] =$parentinfo['id'];
                $parent = $mparent->field(" CONCAT('".$path."/',parent.pic) as parent_url,parent.XM as parent_name,parent.LXDH as parent_tel,user.SJHM as parent_phone,parent.qq as parent_qq,parent.DZXX as parent_email,parent.XBM as parent_sex,parent.birthday as parent_birthday,parent.intro as parent_intro,parent.LXDZ as parent_addr")->join("user ON user.id = parent.user_id") ->where($map)->find();
                $mstudent =M('student');
                //$mparent =M('parent');
                //$studentlist =$mstudent ->field("student.id as student_id , CONCAT('".$path."/',student.pic) as student_url,student.XM as student_name,student.XBM as student_sex,student.school_id,student.birthday as student_birthday,relation as student_relation,student.class_id")->where('parent_id='.$parentinfo['id'])->select();

                $studentlist = $mstudent->field("student.id as student_id , CONCAT('".$path."/',student.pic) as student_url,student.XM as student_name,student.XBM as student_sex,student.school_id,student.birthday as student_birthday,parent_power.relation as student_relation,student.class_id")->join('parent_power ON parent_power.student_id = student.id')->where('parent_power.parent_id='.$parentinfo['id'])->select();
                //echo $mstudent->getlastsql();
                for($i=0;$i<count($studentlist);$i++)
                {
                    $studentlist[$i]['school']=$this ->_loadschool($studentlist[$i]['school_id']);
                    $studentlist[$i]['class']=$this ->_loadclass($studentlist[$i]['class_id']);
                }
                $arr['me']['state']='success';
                $arr['me']['type']='parent';
                $arr['me']['parent']=$parent;
                $arr['me']['parent']['child']=$studentlist;
                


            }
             return $arr;
        }

        private function _uploadpic()
        {
            $key = cookie('key');
            $file = $_FILES['pic']['name'];
            $type =$_REQUEST['type'];
            $id=$_REQUEST['id'];

            if ($type =='teacher')
            {
                $m =M('teacher');
                $count = $m->where('id='.$id)->count();
     
            }
            else if ($type =='parent')
            {
                $m =M('parent');
                $count = $m->where('id='.$id)->count();
            }
            else if ($type =='student')
            {
                $m =M('student');
                $count = $m->where('id='.$id)->count();
            }
            else
            {
                $this->_error(307);
            }

            if ($count==0)
            {
                $this->_error(307);
            }

            import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->savePath =  './uploads/';// 设置附件上传目录
            if(!$upload->upload()) {// 上传错误提示错误信息
                        $this->error($upload->getErrorMsg());
                $this ->_error(308);
            }else{
                $info = $upload->getUploadFileInfo();
                $data['pic'] = 'uploads/'.$info[0]['savename'];
                $arr['uploadpic']['state']='success';
                $m->where('id='.$id)->save($data);
                return $arr;    
            }
        }
        private function _changeME($id)
        {
            $key = cookie('key');
            $keystr =decrypt($key);
            $keyarr =explode('###sc',$keystr);
            $userid = $keyarr[0];
            $phone = $keyarr[1];
            $mm =$_REQUEST['type'];
            if ($mm=='teacher')
            {
                $teacherinfo = loadinfo($userid,1);

               
                if (!empty($_REQUEST['teacher_name']))
                {
                   
                $data['XM'] = $_REQUEST['teacher_name'];
                }
                if (!empty($_REQUEST['teacher_sex']))
                {         
                    $data['XBM'] = $_REQUEST['teacher_sex'];
                }
                if (!empty($_REQUEST['teacher_addr']))
                {         
                    $data['LXDZ'] = $_REQUEST['teacher_addr'];
                }
                if (!empty($_REQUEST['teacher_no']))
                {
                $data['teacher_no'] = $_REQUEST['teacher_no'];
                }
                if (!empty($_REQUEST['teacher_tel']))
                {
                $data['LXDH'] = $_REQUEST['teacher_tel'];
                }
                if (!empty($_REQUEST['teacher_introduce']))
                {
                $data['intro'] = $_REQUEST['teacher_introduce'];
                 }
                 if (!empty($_REQUEST['teacher_email']))
                {
                $data['DZXX'] = $_REQUEST['teacher_email'];
                }

                if (!empty($_REQUEST['teacher_qq']))
                {
                $data['QQ'] = $_REQUEST['teacher_qq'];
                }

                $postphone =$_REQUEST['teacher_phone'];
                $user =M('user');
                $mteacher =M('teacher');
               // $userinfo =$user ->where("SJHM!='".$phone."' and SJHM ='".$postphone."'")->find();
                        //echo $user ->getlastsql();
                // if (empty($userinfo))
                // {
                //     $data1['SJHM'] = $postphone;
                //     $user ->where('id ='.$userid)->save($data1);
                $mteacher ->where('id='.$teacherinfo['id']) ->save($data);
                $arr['changeME']['state']='success';
                //    return $arr;
                // }
                // else
                // {
                //     $this ->_error(309);
                // }
            }
            else
            {
                $parentinfo = loadinfo($userid,2);
              
               
                if (!empty($_REQUEST['parent_name']))
                {
                $data['XM'] = $_REQUEST['parent_name'];
                }
                if (!empty($_REQUEST['parent_tel']))
                {
                $data['LXDH'] = $_REQUEST['parent_tel'];
                }
                if (!empty($_REQUEST['parent_introduce']))
                {
                $data['intro'] = $_REQUEST['parent_introduce'];
                }
                if (!empty($_REQUEST['parent_email']))
                {
                $data['DZXX'] = $_REQUEST['parent_email'];
                }
                if (!empty($_REQUEST['parent_qq']))
                {
                $data['qq'] = $_REQUEST['parent_qq'];
                }
                if (!empty($_REQUEST['parent_sex']))
                {         
                    $data['XBM'] = $_REQUEST['parent_sex'];
                }
                if (!empty($_REQUEST['parent_addr']))
                {         
                    $data['LXDZ'] = $_REQUEST['parent_addr'];
                }

                $postphone =$_REQUEST['parent_phone'];
                $user =M('user');
                $mparent =M('parent');
                // $userinfo =$user ->where("SJHM!='".$phone."' and SJHM ='".$postphone."'")->find();
                // if (empty($userinfo))
                // {
                //     $data1['SJHM'] = $postphone;
                //     $user ->where('id ='.$userid)->save($data1);
                $mparent ->where('id='.$parentinfo['id']) ->save($data);
                $arr['changeME']['state']='success';
                //     return $arr;
                // }
                // else
                // {
                //     $this ->_error(309);
                // }
            }
            return $arr;
            //echo '12312';
        }

        private function _changeStudent($id)
        {
            $student_id = $_REQUEST['student_id'];
            
           
                $pic = $_REQUEST['student_img_name'];
                if (!empty($pic))
                {
                     $data['pic'] =$pic;
                }
                if (!empty($_REQUEST['student_name']))
                {
                $data['XM'] = $_REQUEST['student_name'];
                }
                if (!empty($_REQUEST['student_sex']))
                {
                $data['XBM'] = $_REQUEST['student_sex'];
                }
                if (!empty($_REQUEST['student_birthday']))
                {
                $data['birthday'] = $_REQUEST['student_birthday'];
                }
                if (!empty($_REQUEST['student_relation']))
                {
                $data['relation'] = $_REQUEST['student_relation'];
                }
                $mstudent = M('student');
                $mstudent ->where('id ='.$student_id)->save($data);
                $arr['changeStudent']['state']='success';
                return $arr;
        }

        private function _loadclass($id)
        {
             $mclass = M('class');
               
            $classinfo = $mclass->field('class_name,class.intro as class_introduce,teacher.XM as class_manager') ->join('teacher ON teacher.id = class.teacher_id') ->where('class.id ='.$id)->find();

            return $classinfo;
        }


        private function _loadschool($id)
        {
            $mschool = M('school');
            $map['id'] = $id;
            $schoolinfo = $mschool->field('id as school_id ,XXMC as school_nameC ,XXYWMC as school_nameE,XXDM as school_code,DZXX as school_email,ZYDZ as school_web,XXJJ as school_introduce,JXNY as school_date,CZDH as school_fax,XXDZ as school_address,XZXM as school_headmaster')->where($map)->find();


            return $schoolinfo;

        }
        
        private function _im()
        {
            $key = cookie('key');
            $keystr =decrypt($key);
            $keyarr =explode('###sc',$keystr);
            $userid = $keyarr[0];
            $phone = $keyarr[1];
            
            $send_id = intval($_REQUEST['send_id']);
            $child_id = $_REQUEST['child_id'];
            $receive_id = intval($_REQUEST['receive_id']);
            $type  = $_REQUEST['type'];
            $time = time();
            
            //返回
            $arr['im'] = array('state' => 'success','url' => '','time' => $time); 
            
            if($type == 'voice' || $type == 'img'){
                
                $file = $_FILES['content']['name'];
                
                import('ORG.Net.UploadFile');
                $upload = new UploadFile();// 实例化上传类
                $upload->maxSize  = 3145728 ;// 设置附件上传大小
                $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','amr','mp3');// 设置附件上传类型
                $upload->savePath =  './uploads/';// 设置附件上传目录
                if(!$upload->upload()) {// 上传错误提示错误信息
                    $this->error($upload->getErrorMsg());
                    $this ->_error(308);
                }else{
                    $info = $upload->getUploadFileInfo();
                    $content = C('PATH').'/uploads/'.$info[0]['savename'];
                }
                
                $arr['im']['url']= $content; 
            }else{
                $content = $_REQUEST['content'];
            }
            
            //if($userid == $send_id){
            
                $message = M("message");
                $user =M('user');
                
                
                $data["send_id"] = $send_id;
                $data["receive_id"] = $receive_id;
                $data["child_id"] = $child_id;  
                $data["type"] = $type;
                $data["content"] = $content;    
                $data["time"] = $time;
                $data["state"] = 0;
                             
                
                $condition['id'] = $receive_id;
                $userinfo = $user->field('systype,token,SJHM')->where($condition)->limit('0,1')->select();
                
                //先推送
                if($userinfo[0]['systype']=='android'){
                    require_once './SAM/php_sam.php';
                    $conn = new SAMConnection();
                    $conn->connect(SAM_MQTT, array(SAM_HOST => 'www.appbaba.com', SAM_PORT => 1883));  
                    $androidcon = '{"type":"'.$type.'","child_id":"'.$child_id.'","send_id":"'.$send_id.'","receive_id":"'.$receive_id.'","time":"'.$data["time"].'","content":"'.$content.'"}';                        
                    $msgCpu = new SAMMessage($androidcon);
                    $conn->send('topic://999/'.$userinfo[0]['token'], $msgCpu);
                    $conn->disconnect(); 
                    
                }elseif($userinfo[0]['systype']=='ios'){
                    $body['aps'] = array( 'alert' => $content,  'sound' => 'default' ); 
                    $body['action'] = array( 'type' => $type,'child_id' => $child_id,  'send_id' => $send_id,  'receive_id' => $receive_id, 'content' => $content, 'time' =>$data["time"]  ); 
                    $ctx = stream_context_create();
                    $pem = 'f:\diyapp\jd\SAM\jiaodian.pem';
                    stream_context_set_option($ctx,"ssl","local_cert",$pem);
                    $fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
                    if (!$fp) {/*  echo "Failed to connect $err $errstrn"; */
                        $arr['im']['state'] = 'error';
                    }else{
                        $payload = json_encode($body); 
                        
                        $msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $userinfo[0]['token'])) . pack("n",strlen($payload)) . $payload;
                        $result = fwrite($fp, $msg, strlen($msg));
                        if (!$result){
                            //echo "sending error : dev:".$userinfo[0]['token'];
                            $arr['im']['state'] = 'error';
                        }
                    }
                }elseif($userinfo[0]['systype']==''){
                    $sms = $this->sendsms($userinfo[0]['SJHM'],$content);
                    if($sms!==1){
                        $arr['im']['state'] = 'error';
                    } 
                }
                
                //确保成功再插入
                if($arr['im']['state'] == 'success'){
                    $lastInsId = $message->add($data);
                }
                
                return $arr; 
            /* }else{
                $this ->_error(306);
            } */
        }

        private function _imOnRead()
        {
            $key = cookie('key');
            $keystr =decrypt($key);
            $keyarr =explode('###sc',$keystr);
            $userid = $keyarr[0];
            $phone = $keyarr[1];
            $message_id = intval($_REQUEST['message_id']);
            
            if($userid){
                $message = M("message");
                $messdate = $message->where('(receive_id='.$userid.' OR send_id='.$userid.') AND id>'.$message_id)->select();
                
                return $messdate; 
            }else{
                $this ->_error(306);
            }
        }
        
        public function attence()
        {
            // $key = cookie('key');
            // $keystr =decrypt($key);
            // $keyarr =explode('###sc',$keystr);
            // $userid = $keyarr[0];

            $time =time();
            $starttime = $time -(60*60*24*30);

            for ($i=$starttime; $i <= $time ; $i=$i+(60*60*24)) { 
                //echo $i,'<br>';
                $datelist[$i] =date('Y-m-d',$i);
                //echo date('Y-m-d',$i),'<br>';
                # code...

            }
            $this->assign('today',date('Y-m-d',$i-(60*60*24)));

            $this->assign('datelist',$datelist);

            $userid = $_REQUEST['id'];
            $parentinfo = loadinfo($userid,2);
            $f=$_REQUEST['f'];
            $this->assign('f',$f);
                
           
            $mstudent =M('student');
            $mclass =M('class');
            $lesson_type =M('lesson_type');
            $studentlist = $mstudent ->field('student.*')->join('parent_power on parent_power.student_id =student.id')->where('parent_power.parent_id='.$parentinfo['id'])->select();
            foreach ($studentlist as $stl) {
                $stlid .= $stl['id'].',';
            }
            $stlid = rtrim($stlid,',');
            $this->assign('studentlist',$studentlist);
            $this->assign('id',$userid);
            $time= $_REQUEST['time'];
            
            
            $timeint =strtotime($time);
            $student_id =$_REQUEST['student_id'];
            if (!empty($timeint))
            {
                $week = date("w",$timeint);
                if ($week==0)
                {
                    $week =7;
                }
                $map['FBSJ'] = $timeint;

            }
            if (!empty($student_id))
            {
                $map['student_id'] =$student_id;
            }
            else
            {
                $map['student_id'] = array('in',$stlid);
            }
            $mstudent = M('student');
            $studentinfo =$mstudent->find($student_id);

            $this->assign('time',$timeint);
            $this->assign('week',$week);
            $this->assign('student_id',$student_id);
            $this->assign('class_id',$studentinfo['class_id']);
            $lesson_type =M('lesson_type');
            //$lessonlist =$lesson_type->where('school_id='.$studentinfo['school_id'])->order(' num asc')->select();
                //print_r($lessonlist);




                $mstudent_attence =M('student_attence');
                
                $lessonlist = $mstudent_attence->where($map)->order('FBSJ desc')->select();
                $this->assign('lessonlist',$lessonlist);

            


            $this->display('attence_new');
        }

        public function result()
        {
            $type =$_REQUEST['type'];
            if ($type =='teacher')
            {
                // $key = cookie('key');
                // $keystr =decrypt($key);
                // $keyarr =explode('###sc',$keystr);
                $userid = $_REQUEST['id'];
                $this->assign('id',$userid);
                $teacherinfo = loadinfo($userid,1);
                $teacher_id =$teacherinfo['id'];
                $map['school_id']=$teacherinfo['school_id'];
                $school_type =getinfo($map['school_id'],'school','school_type_id');
                $level= M('level');
                $levellist = $level->where('school_type_id = '.$school_type)->select();
                $class_id =$_REQUEST['class_id'];
                if (!empty($_REQUEST['name']))
                {
                   $mapst['XM'] = array('like','%'.$_REQUEST['name'].'%');
                }
                if(!empty($class_id))
                {
                    $mapst['class_id']=$class_id;
                }
                $exam_id =$_REQUEST['exam_id'];
                $mstudent = M('student');
                $stlist = $mstudent ->where($mapst)->select();
                $this->assign('levellist',$levellist);
                $mclass=M('class');
                $classlist =$mclass->field('id')->where('teacher_id = '.$teacher_id)->select();
                foreach ($classlist as $clt) {
                    $cllist .= $clt['id'].',';
                }

                $mtp = M('teacher_power');
                $plist = $mtp->where('teacher_id ='.$teacher_id)->select();
                foreach ($plist as $plt) {
                    $tplist .= $plt['class_id'].',';
                }
                $clslist = rtrim($tplist.$cllist,',');
                $finalclasslist = $mclass->where('id in('.$clslist.')')->select();
                $this ->assign('finalclasslist',$finalclasslist);
                
                $this->assign('stlist',$stlist);
                $exam_id =$_REQUEST['exam_id'];
                
                $mexam =M('exam');
                $examlist =$mexam->where($map)->select();
                $this->assign('examlist',$examlist);


                if ($exam_id)
                {
                    $examinfo =$mexam->find($exam_id);
                }
                if ($class_id)
                {
                    $classinfo =$mclass->find($class_id);
                }
                $msubject =M('subject');
                $subject =$msubject->where($map)->select();
                for($i=0;$i<count($subject);$i++)
                {
                    $count= loadsubinfo($exam_id,$subject[$i]['id'],$class_id);
                    if ($count ==0)
                    {
                        $subject[$i]['KCMC'] ='';
                    }
                }
               // print_r($subject);
               // exit;
        
                //查询成绩
                //echo $mstudent_result->getlastsql();
                $this->assign('type',$type);
                $this->assign('classinfo',$classinfo);
                $this->assign('subject',$subject);
                $this->assign('exam_id',$exam_id);
                $this->assign('examinfo',$examinfo);
                $this ->assign('levellist',$levellist);
                $this->display('score');
            }
            if ($type == 'parent')
            {
                $userid = $_REQUEST['id'];
                $student_id = $_REQUEST['student_id'];
                $f=$_REQUEST['f'];
                $this->assign('f',$f);
                $parentinfo = loadinfo($userid,2);
                $parent_id =$parentinfo['id'];
                $this->assign('type',$type);
                $mstudent =M("student");
                $this->assign('id',$userid);
                $map['parent_id'] = $parent_id;
                $map['id'] = $student_id;
                $this->assign('student_id',$student_id);
                if (!empty($student_id))
                {
                    //$mexam=M('exam');
                    $mapex['school_id'] = getinfo($student_id,'student','school_id');
                    $mexam =M('exam');
                    $examlist =$mexam->where($mapex)->select();
                    $this->assign('examlist',$examlist);

                    //$examlist =$mexam ->where()
                }
                $studentlist = $mstudent ->where($map)->select();
                $this->assign('studentlist',$studentlist);
                $this->display('score_p');
            }
        }

        public function loadexam()
        {
            $map['class_id'] = $_REQUEST['clid'];
            $map['term_id'] = $_REQUEST['teid'];
            $m=M('exam');
            $list = $m ->where($map)->select();
            echo json_encode($list);
        }
        public function loadterm()
        {
            $map['class_id'] = $_REQUEST['id'];
            $m=M('term');
            $list =$m->where($map)->select();
            echo json_encode($list);

        }

        public function _postAttence()
        {
            $no= $_REQUEST['lesson_no'];
            $class_id =$_REQUEST['class_id'];
            $time = $_REQUEST['time'];
            $student_id =$_REQUEST['student_id'];
            $student_id = trim($student_id,'[');
            $student_id = trim($student_id,']');
            $student_id = trim($student_id,'"');

            if (empty($class_id) or empty($time) or empty($student_id) or empty($no))
            {
                $this ->_error(307);
            }

            $week = date("w",$time);
            if ($week==0)
            {
                $week =7;
            }
            //echo $week;
            $map['class_id'] = $_REQUEST['class_id'];
            $map['XXZT'] = 1;
            $mschedule =M('schedule');
            $mlesson =M('lesson');
            $schedule = $mschedule ->where($map)->find();
            $map1['schedule_id'] = $schedule['id'];
            $map1['week_day'] = $week;
            $map1['lesson_type_id'] = $no;
            $lesson = M('lesson');
            $lessoninfo =$lesson ->where($map1) ->find();
            //echo $lesson->getlastsql();
            $dtime = date('Y-m-d',$time);
            $dtime = strtotime($dtime);
            $mapal['lesson_id']=$lessoninfo['id'];

            



            $mapal['FBSJ']=$dtime;
            $mapal['class_id']=$map['class_id'];
            $mstudent_attence =M('student_attence');
            if ($mstudent_attence->where($mapal)->count()>0)
            {
                $this ->_error(313);
            }

            if (empty($lessoninfo))
            {
                $this ->_error(310);
            }
            else
            {
                $mstudent=M('student');
                $studentlist =$mstudent->where('class_id='.$map['class_id'])->select();
                $starr =explode(',',$student_id);
                
                foreach ($studentlist as $stl) {
                    
                    $map2['FBSJ'] =$dtime;
                    $map2['student_id'] =$stl['id'];
                    $map2['lesson_id'] = $lessoninfo['id'];
                    $map2['class_id'] = $map['class_id'];

                    $subject_id = getinfo($lessoninfo['id'],'lesson','subject_id');
                    $subject_name = getinfo($subject_id,'subject','KCMC');
                    $map2['subject_name'] = $subject_name;

                    $lesson_type_id = getinfo($lessoninfo['id'],'lesson','lesson_type_id');
                    $lessontype = getinfo($lesson_type_id,'lesson_type','type_name');
                    $map2['type_name'] = $lessontype;

                    if (in_array($stl['id'], $starr))
                    {
                            
                        $map2['status'] = 1;
                            
                    }
                    else
                    {
                        $map2['status'] = 0;
                    }
                    $mstudent_attence->add($map2);
                    # code...
                }
                // $starr =explode(',',$student_id);

                // for($i=0;$i<count($starr);$i++)
                // {
                //     $dtime = date('Y-m-d',$time);
                //     $dtime = strtotime($dtime);
                //     $map2['FBSJ'] =$dtime;
                //     $map2['student_id'] =$starr[$i];
                //     $map2['lesson_id'] = $lessoninfo['id'];
                //     $map2['class_id'] = $class_id;

                //     $mstudent_attence =M('student_attence');
                //     if ($mstudent_attence->where($map2)->count() == 0)
                //     {
                //         $mstudent_attence->add($map2);
                //     }
                // }
            $arr['postAttence']['state']='success';
            return $arr;
            }



  //          echo $week;

        }
        public function alllesson()
        {
            $userid = $_REQUEST['id'];
            $this->assign('id',$userid);
            $type =$_REQUEST['type'];
            $class_id =$_REQUEST['class_id'];
            $this->assign('class_id',$class_id);
            $this->assign('type',$type);
            if ($type =='teacher')
            {
                $teacherinfo = loadinfo($userid,1);
                $idlist =loadpower(3,$teacherinfo['id']);
                //echo $idlist;
                $mclass =M('class');
                $classlist = $mclass->where("id in (".$idlist.")")->select();
                //print_r($classList);
                //exit;
                $this->assign('classlist',$classlist);
                $lesson_type =M('lesson_type');
                $lessonlist =$lesson_type->where('school_id='.$teacherinfo['school_id'])->select();
                foreach ($lessonlist as $ltlist) {
                    $time = $ltlist['starttime'];
                    $timearr =explode(':',$time);
                    if ($timearr[0]>12)
                    {
                        $pmnum=$ltlist['num'];
                        break;
                    }

                    # code...
                } 
                
                $amlist = $lesson_type->where('school_id='.$teacherinfo['school_id'].' and num<'.$pmnum)->order('num asc')->select();
                $pmlist = $lesson_type->where('school_id='.$teacherinfo['school_id'].' and num>='.$pmnum)->order('num asc')->select();

                $this->assign('amlist',$amlist);
                $this->assign('pmlist',$pmlist);
               
                $this->display('lesson_allnew');
            }

        }
        public function lesson()
        {
            // $key = cookie('key');
            // $keystr =decrypt($key);
            // $keyarr =explode('###sc',$keystr);
            $userid = $_REQUEST['id'];
            $type =$_REQUEST['type'];
            $this->assign('type',$type);
            if ($type =='teacher')
            {
                $teacherinfo = loadinfo($userid,1);
                if (empty($teacherinfo))
                {
                    $this->_error(307);
                }
                $lesson_type =M('lesson_type');
                $lessonlist =$lesson_type->where('school_id='.$teacherinfo['school_id'])->select();
                foreach ($lessonlist as $ltlist) {
                    $time = $ltlist['starttime'];
                    $timearr =explode(':',$time);
                    if ($timearr[0]>12)
                    {
                        $pmnum=$ltlist['num'];
                        break;
                    }

                    # code...
                } 
                
                $amlist = $lesson_type->where('school_id='.$teacherinfo['school_id'].' and num<'.$pmnum)->order('num asc')->select();
                $pmlist = $lesson_type->where('school_id='.$teacherinfo['school_id'].' and num>='.$pmnum)->order('num asc')->select();

                $this->assign('amlist',$amlist);
                $this->assign('pmlist',$pmlist);
               
                $this->assign('teacher_id',$teacherinfo['id']);
                $this->display('lesson_new');
            }
            if ($type=='parent')
            {

                $parentinfo = loadinfo($userid,2);
                
                if (empty($parentinfo))
                {
                    $this->_error(307);
                }
                $f=$_REQUEST['f'];
                $this->assign('f',$f);
                $mstudent =M('student');
                $mclass =M('class');
                $lesson_type =M('lesson_type');
                $studentlist = $mstudent ->field('student.*')->join('parent_power on parent_power.student_id =student.id')->where('parent_power.parent_id='.$parentinfo['id'])->select();
                $this->assign('studentlist',$studentlist);
                

                $this->display('lesson_np');
            }


        }

        public function lesson_parent()
        {
            $id=$_REQUEST['student_id'];
            $mstudent=M('student');
            $studentinfo =$mstudent->find($id);
            $lesson_type =M('lesson_type');
            $school_id = $studentinfo['school_id'];
            $class_id = $studentinfo['class_id'];
            $lessonlist =$lesson_type->where('school_id='.$school_id)->select();
                foreach ($lessonlist as $ltlist) {
                    $time = $ltlist['starttime'];
                    $timearr =explode(':',$time);
                    if ($timearr[0]>12)
                    {
                        $pmnum=$ltlist['num'];
                        break;
                    }

                    # code...
                } 
                
                $amlist = $lesson_type->where('school_id='.$school_id.' and num<'.$pmnum)->order('num asc')->select();
                $pmlist = $lesson_type->where('school_id='.$school_id.' and num>='.$pmnum)->order('num asc')->select();
                $this->assign('amlist',$amlist);
                $this->assign('pmlist',$pmlist);
                $this->assign('class_id',$class_id);
                $this->display('lesson_parent');
        }

        public function viewresult()
        {
            $stid =$_REQUEST['stid'];   
            $exid =$_REQUEST['exid'];
            $f=$_REQUEST['f'];
            $this->assign('f',$f);
                
            $mexam =M('exam');
            $this->assign('exam_id',$exid);
            $mstudent=M('student');
            $mstudent_result=M('student_result');
            $class_id = getinfo($stid,'student','class_id');
            $map['exam_id']=$exid;
            $map['student_id']=$stid;
            $result =$mstudent_result->where($map)->sum('FSLKSCJ');
            $resultlist = $mstudent_result ->where($map)->select();
            $this->assign('resultlist',$resultlist);
            $this->assign('class_id',$class_id);
            $this->assign('student_id',$stid);
            $this->assign('result',$result);

            $examinfo =$mexam->where('id='.$exid)->find();
            $this->display('score_info');

        }

        public function view()
        {
            $type =$_REQUEST['type'];
            $id =$_REQUEST['id'];
            switch ($type) {
                case 'schproclamation':
                    # code...
                    $m=M('school_proclamation');
                    $info =$m->field('proclamation_type.proclamation_type_name,school_proclamation.title,school_proclamation.content,school_proclamation.FBSJ,teacher.XM')->join(' proclamation_type ON school_proclamation.proclamation_type_id = proclamation_type.id') ->join('teacher ON teacher.id = school_proclamation.teacher_id')->where('school_proclamation.id='.$id)->find();
                    $this->assign('info',$info);
                    $this->display('pview');
                    break;

                case 'clsproclamation':
                    $m=M('class_proclamation');
                    $info =$m->field('proclamation_type.proclamation_type_name,class_proclamation.title,class_proclamation.content,class_proclamation.FBSJ,teacher.XM')->join(' proclamation_type ON class_proclamation.proclamation_type_id = proclamation_type.id') ->join('teacher ON teacher.id = class_proclamation.teacher_id')->where('class_proclamation.id='.$id)->find();
                    $this->assign('info',$info);
                    $this->display('pview');
                    # code...
                    break;

                case 'homework':
                    $m=M('student_work');
                     $info =$m ->join('teacher ON teacher.id = student_work.teacher_id')->where('student_work.id='.$id) ->find();
                    $this->assign('info',$info);
                    $this->display('wview');
                    break;
                default:
                    # code...
                    break;
            }

        }
    

}